package com.example.demo07.contorl;
import com.example.demo07.dao.UserDao;
import com.example.demo07.entity.Response;
import com.example.demo07.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

    @RestController
    @RequestMapping("/user")
    public class RegisterController {
        @Autowired
        UserDao userDao;
        @PostMapping("/register")
        public Response register(@RequestBody User user){
            System.out.println(user.getUsername()+user.getPassword());
            userDao.insertUser(user);
            return null;
        }
        @PostMapping("/login")
        public Response login(@RequestBody User user){
            System.out.println(user.getUsername()+user.getPassword());
            User user1 = userDao.findByName(user);
            if (user1!=null){
                if (user1.getPassword().equals(user.getPassword())){
                    System.out.println("登录成功");
                    return Response.ok((List<User>) user1);
                }
                return Response.fail(400,"密码错误");
            }return Response.fail(400,"用户不存在");
        };
        @GetMapping("/users")
        public Response users(){
            System.out.println("查询所有用户");
            List<User> user =  userDao.FindAll();
            System.out.println(user);
            if (userDao.FindAll()!=null){
                return Response.ok(user);
            }return Response.fail(400,"用户不存在");

        }
    }

